import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/floating_drag_action_button.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommTextFieldWidget extends StatefulWidget {
  Color? cursorColor;
  TextInputType? keyboardType;
  String? defaultCont;
  String? hintTxt;
  String? lableText;
  bool? isShowLable;
  TextStyle? hintStyle;
  TextStyle? labelStyle;
  TextStyle? style;
  Function? onChanged;
  Widget? prefixIcon;
  bool? isShowPrefixIcon;
  EdgeInsets? contentPadding;
  int? maxLength;
  List<TextInputFormatter>? inputFormatters;

  CommTextFieldWidget({super.key,
    this.cursorColor,
    this.keyboardType,
    this.defaultCont,
    this.hintStyle,
    this.labelStyle,
    this.style,
    this.lableText,
    this.isShowLable,
    this.hintTxt,
    this.onChanged,
    this.prefixIcon,
    this.isShowPrefixIcon,
    this.contentPadding,
    this.maxLength,
    this.inputFormatters,
  });

  @override
  State<CommTextFieldWidget> createState() => _CommTextFieldWidgetState();
}

class _CommTextFieldWidgetState extends State<CommTextFieldWidget> {
  late TextEditingController _editingController;
  RxBool contCanClear = false.obs;
  @override
  void initState() {
    super.initState();
    _editingController=TextEditingController(text: widget.defaultCont);
    contCanClear.value = (widget.defaultCont??'').isNotEmpty;
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() => TextField(
      controller: _editingController,
      onChanged: (value){
        if(widget.onChanged!=null)widget.onChanged!(value);
        if(value.isNotEmpty){
          contCanClear.value = true;
        }else{
          contCanClear.value = false;
        }
      },
      maxLength: widget.maxLength??21,
      style: widget.style??TextStyle(
          fontSize: px_18.sp,
          color: color_333333
      ),
      inputFormatters: widget.inputFormatters,
      decoration: InputDecoration(
        counter: SizedBox(),
        contentPadding: widget.contentPadding??EdgeInsets.symmetric(vertical: px_2.h),
        labelText: (widget.isShowLable??true)?(widget.lableText??"${string_account}"):null,
        labelStyle: widget.labelStyle??TextStyle(
            fontSize: px_16.sp,
            color: color_999999
        ),
        hintText: widget.hintTxt??'${string_please_input}${string_account}',
        hintStyle: widget.hintStyle??TextStyle(
            fontSize: px_16.sp,
            color: color_999999
        ),
        // floatingLabelBehavior: FloatingLabelBehavior.never,
        prefixIcon: (widget.isShowPrefixIcon??true)?
        (widget.prefixIcon??
            Icon(
              Icons.person,
              size: px_32.sp,
              color: color_999999,
            )):null,
        suffixIcon: contCanClear.value ? IconButton(
          icon: Icon(
            Icons.cancel,
            size: px_23.sp,
            color: color_999999,
          ),
          onPressed: (){
            if(_editingController!=null)_editingController.clear();
            contCanClear.value = false;
            if(widget.onChanged!=null)widget.onChanged!('');
          },
        ) : null,
        enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(px_6.sp),
            borderSide: BorderSide(
                color: color_cccccc
            )
        ),
        focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(px_6.sp),
            borderSide: BorderSide(
                color: color_cccccc
            )
        ),
        // filled: true,
        // fillColor: Color(0xFFF8F8F8),
      ),

      cursorColor: widget.cursorColor,
      keyboardType: widget.keyboardType??TextInputType.text,
    ));
  }
}
